package com.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youxuan.enums.AppHttpCodeEnum;
import com.youxuan.enums.GlobalConst;
import com.youxuan.exception.SystemException;
import com.youxuan.mapper.GoodsMapper;
import com.youxuan.pojo.entity.Goods;
import com.youxuan.pojo.entity.LoginUser;
import com.youxuan.pojo.entity.ResponseResult;
import com.youxuan.pojo.vo.GoodsListVo;
import com.youxuan.pojo.vo.GoodsVo;
import com.youxuan.pojo.vo.PageVo;
import com.youxuan.service.GoodsService;
import com.youxuan.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ResponseResult<PageVo<Goods>> getList(Integer pageNum, Integer pageSize, Integer categoryId) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(categoryId)){
            queryWrapper.eq(Goods::getCId,categoryId);
        }else {
            queryWrapper.orderByAsc(Goods::getGoodsName);
        }
        Page<Goods> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        PageVo<Goods> pageVo = new PageVo<>();
//        List<GoodsListVo> goodsListVos = BeanCopyUtils.copyList(page.getRecords(), GoodsListVo.class);

        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addSingle(Goods goods) {
        if (!isAdmin()) {
            new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        goods.setId(null);
        save(goods);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult removeSingle(Integer goodsId) {
        if (!isAdmin()) {
            new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Goods::getId,goodsId);
        updateWrapper.set(Goods::getDelFlag,GlobalConst.delete);
        goodsMapper.update(null,updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateSingle(Goods goods) {
        if (!isAdmin()) {
            new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        updateById(goods);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<List<Goods>> findGoods(String antistop) {

        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getGoodsName,antistop);

        List<Goods> goods = goodsMapper.selectList(queryWrapper);

        return ResponseResult.okResult(goods);
    }

    @Override
    public Goods getGoods(Long goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        return goods;
    }

    public boolean isAdmin(){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext()
                                                    .getAuthentication().getPrincipal();
        String type = loginUser.getUser().getType();
        return type.equals(GlobalConst.Admin);
    }
}
